package com.atu.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class ServletContext_Map_1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取 ServletContext 域对象
        ServletContext context = getServletContext();
        System.out.println(context);
        System.out.println("保存之前: Context1 获取 key1 的值是:"+ context.getAttribute("key1"));
        System.out.println("保存之前: Context1 获取 key2 的值是:"+ context.getAttribute("key2"));
        /**
         *  A.setAttribute(String,String):
         *  由实例对象A设置键值对的值，就应该由A去除键值对的值。这样可以实现值在不同程序中的传递；否则获取不到原本设置的值
         */
        context.setAttribute("key1","value1");
        context.setAttribute("key2","value2");
        System.out.println("Context1 中获取域数据 key1 的值是:"+ context.getAttribute("key1"));
        System.out.println("Context1 中获取域数据 key2 的值是:"+ context.getAttribute("key2"));
        System.out.println(getServletConfig());
    }
}
